import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by lanouhn on 17/7/18.
 */
public class ImageDoload {

        public static void main(String[] args)
        {
            //设置网络连接变量
            HttpURLConnection connection = null;
            //设置接收流对象


            try{
                String ImageUrl = "http://oss.laohucaijing.com/UserFiles/Image/201603/20160330111155221.png";
                URL url = new URL (ImageUrl);

                connection = (HttpURLConnection) url.openConnection ();

                int code = connection.getResponseCode ();

                if(code == 200)
                {
                    System.out.println ("打开成功！");
                    BufferedImage image = ImageIO.read (connection.getInputStream ());

                    String path = "/Users/lanouhn/Desktop/result1.txt";
                    File fl = new File (path);
                    FileWriter fw = new FileWriter (fl);
                    ImageIO.write (image,"jpg",fl);
                }
                else
                {
                    System.out.println ("打开失败！");
                }
            }catch (MalformedURLException e)
            {
                e.printStackTrace ();
            }catch (IOException e)
            {
                e.printStackTrace ();
            }finally {
                if(connection != null)
                {
                    connection.disconnect ();
                }
    //
            }
        }
}
